import { _decorator, Component, Node, Enum, Label, UIOpacity } from 'cc';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { PlatformType, SDKSystem } from '../../Init/SystemSDK/SDKSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
const { ccclass, property } = _decorator;
export enum LvupPropType {
    HP,
    MoveSpd,
    Num
}
@ccclass('lvupPropCmp')
export class lvupPropCmp extends Component {
    @property({ type: Enum(LvupPropType) })
    propType: LvupPropType = LvupPropType.MoveSpd;

    @property(Label)
    addLabel: Label = null;

    @property(Label)
    propLabel: Label = null;

    @property(Label)
    costLabel: Label = null;

    addNum = 0;
    propNum = 0;
    costNum = 0;
    _isVideo = false;
    _isHideVideoIco = false;

    _opacityCmp: UIOpacity = null;
    _isShowVideoIcon = true;

    onLoad() {
        this._opacityCmp = this.node.getComponent(UIOpacity);
        if (!this._opacityCmp) {
            this._opacityCmp = this.node.addComponent(UIOpacity);
            this._opacityCmp.opacity = 255;
        }

        EventManager.on(EventTypes.GameEvents.UserAssetsChanged, this.freshData, this);
    }

    onEnable() {
        // this._isShowVideoIcon = Number(wxsdk.GetConfigById('VideoIconStatus')) == 0; //0-显示 1-隐藏     
        this._opacityCmp.opacity = 255;
        this.setData();
    }

    setData() {
        const _cfg = GlobalConfig.RolePropCfg;
        let d = StorageSystem.getData();
        let isMax = false;
        switch (this.propType) {
            case LvupPropType.HP:
                this.addNum = _cfg.getHpStepByLv(d.userAssets.props.hpLv);
                this.propNum = _cfg.getHpByLv(d.userAssets.props.hpLv);
                this.costNum = _cfg.getHPCost(d.userAssets.props.hpLv);

                this.addLabel.string = '+' + Math.floor(this.addNum * 100 / _cfg.hp.default).toFixed(0) + '%';
                this.propLabel.string = Math.floor((d.userAssets.props.hpLv - 1) * 10 + 100).toFixed(0) + '%';
                this.costLabel.string = this.costNum.toFixed(0);
                break;
            case LvupPropType.MoveSpd:
                this.addNum = _cfg.getMoveSpdStepBylv(d.userAssets.props.moveSpdLv);
                this.propNum = _cfg.getMoveSpdByLv(d.userAssets.props.moveSpdLv);
                this.costNum = _cfg.getMoveSpdCost(d.userAssets.props.moveSpdLv);

                if (this.propNum >= _cfg.moveSpd.max) {
                    this.addLabel.string = '已达上限';
                    isMax = true;
                } else {
                    this.addLabel.string = '+' + (Math.floor(this.addNum * 1000 / _cfg.moveSpd.default) / 10).toFixed(0) + '%';
                }
                this.propLabel.string = Math.floor((d.userAssets.props.moveSpdLv - 1) * 10 + 100).toFixed(0) + '%';
                this.costLabel.string = this.costNum.toFixed(0);
                break;
            case LvupPropType.Num:
                this.addNum = _cfg.num.step;
                this.propNum = _cfg.getNumRateByLv(d.userAssets.props.numLv);
                this.costNum = _cfg.getNumRateCost(d.userAssets.props.numLv);

                if (this.propNum >= _cfg.num.max) {
                    this.addLabel.string = '已达上限';
                    isMax = true;
                } else {
                    this.addLabel.string = '+' + Math.floor(this.addNum * 100 / _cfg.num.default).toFixed(0) + '%';
                }
                this.propLabel.string = Math.floor(d.userAssets.props.numLv * 100).toFixed(0) + '%';
                this.costLabel.string = this.costNum.toFixed(0);
                break;
            default:
                break;
        }
        //切换视频图标
        this._isVideo = this.costNum > d.userAssets.asset;
        let gold = this.node.getChildByName('gold');
        let video = this.node.getChildByName('videoIcon');


        this._isHideVideoIco = this._isShowVideoIcon;

        switch (SDKSystem._curPlatform) {
            case PlatformType.WXMiniGame:
            case PlatformType.PCMiniGame:
            case PlatformType.TTMiniGame:

                // gold.active = !this._isVideo && !isMax || this._isHideVideoIco && !isMax;
                // video.active = !this._isHideVideoIco && this._isVideo && !isMax;

                if (this._isShowVideoIcon) {
                    gold.active = !isMax && !this._isVideo;
                    video.active = this._isVideo && !isMax;
                } else {
                    gold.active = !isMax;
                    video.active = false;
                }
                break;
            default:
                break;
        }
    }

    onClick() {
        const _cfg = GlobalConfig.RolePropCfg;
        let data = StorageSystem.getData();

        switch (this.propType) {
            case LvupPropType.HP:

                // wx事件
                // wxsdk.GetAdById('LvupHpClick');

                if (data.userAssets.asset < this.costNum) {
                    //视频
                    this.randomPop(this.propType, () => {
                        StorageSystem.setData((d) => {
                            d.userAssets.props.hpLv++;
                        }, true);
                        EventManager.emit(EventTypes.RoleEvents.ShowSurprise);
                        this.freshData();
                        AudioSystem.playEffect(AudioEnum.Lvup);
                    })
                } else {
                    StorageSystem.setData((d) => {
                        d.userAssets.props.hpLv++;
                        d.userAssets.asset -= this.costNum;
                    }, true);
                    EventManager.emit(EventTypes.RoleEvents.ShowSurprise);
                    StorageSystem.updateToAssets();
                    AudioSystem.playEffect(AudioEnum.Lvup);
                }

                break
            case LvupPropType.MoveSpd:

                // wx事件
                // wxsdk.GetAdById('LvupSpdClick');

                if (this.propNum >= _cfg.moveSpd.max) {
                    //弹出提示
                    EventManager.emit(EventTypes.GameEvents.ShowTips, '已达最大速度!');
                    return;
                }

                if (data.userAssets.asset < this.costNum) {
                    //视频
                    this.randomPop(this.propType, () => {
                        StorageSystem.setData((d) => {
                            d.userAssets.props.moveSpdLv++;
                        }, true);
                        EventManager.emit(EventTypes.RoleEvents.ShowSurprise);
                        this.freshData();
                        AudioSystem.playEffect(AudioEnum.Lvup);
                    })

                } else {
                    StorageSystem.setData((d) => {
                        d.userAssets.props.moveSpdLv++;
                        d.userAssets.asset -= this.costNum;
                    }, true);
                    EventManager.emit(EventTypes.RoleEvents.ShowSurprise);
                    StorageSystem.updateToAssets();
                    AudioSystem.playEffect(AudioEnum.Lvup);
                }

                break;
            case LvupPropType.Num:
                // wx事件
                // wxsdk.GetAdById('LvupZombieClick');

                if (this.propNum >= _cfg.num.max) {
                    //弹出提示
                    EventManager.emit(EventTypes.GameEvents.ShowTips, '已达最大数量!');
                    return;
                }

                if (data.userAssets.asset < this.costNum) {
                    //视频
                    this.randomPop(this.propType, () => {
                        StorageSystem.setData((d) => {
                            d.userAssets.props.numLv++;
                        }, true);
                        EventManager.emit(EventTypes.RoleEvents.CreateZombie);
                        AudioSystem.playEffect(AudioEnum.Lvup);
                        this.freshData();
                    })
                } else {
                    StorageSystem.setData((d) => {
                        d.userAssets.props.numLv++;
                        d.userAssets.asset -= this.costNum;
                    }, true);
                    EventManager.emit(EventTypes.RoleEvents.CreateZombie);
                    StorageSystem.updateToAssets();
                    AudioSystem.playEffect(AudioEnum.Lvup);
                }
                break;
            default:
                break;
        }

    }

    freshData() {
        if (!this.node.activeInHierarchy) return;
        this.setData();
    }

    getCostByLv(start, lv, rate) {
        let sumRate = 1;
        for (let i = 1; i < lv; i++) {
            sumRate *= (1 + rate);
        }
        return Math.floor(sumRate * start);
    }

    //#region --------------------广告-------------------------

    //金币不足时
    private randomPop(type, successCb) {
        switch (SDKSystem._curPlatform) {
            case PlatformType.PCMiniGame:
            case PlatformType.WXMiniGame:
            case PlatformType.TTMiniGame:
                if (GlobalTmpData.isMiscontact) {
                    this.TTPop(type, successCb);
                } else {
                    this.WxPop(type, successCb);
                }
                break;
            // case PlatformType.TTMiniGame:
            //     this.TTPop(type, successCb);
            //     break;
            default:
                break;
        }
    }

    private TTPop(type, successCb) {
        this._isHideVideoIco = false;
        //观看视频
        EventManager.emit(EventTypes.SDKEvents.ShowVideo, {
            success: () => {
                //观看视频成功
                successCb && successCb();
            },
            fail: () => {
                this._isHideVideoIco = false;
                this.setData();
            },
            cancel: () => {
                this._isHideVideoIco = false;
                this.setData();
            },
        })
    }
    //不足时点击未弹出窗口的次数
    private WxPop(type, successCb) {
        this._isHideVideoIco = true;
        //弹出
        UISystem.showUI(UIEnum.FreeGoldUI);
    }

    //#endregion
}
